package com.ibangnuo.manage.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ibangnuo.framework.domain.User;
import com.ibangnuo.framework.domain.UserRole;
import com.ibangnuo.framework.domain.ext.UserExt;
import com.ibangnuo.framework.domain.request.employee.EmployeeRequest;
import com.ibangnuo.framework.domain.request.user.UserQueryRequest;
import com.ibangnuo.framework.model.response.*;
import com.ibangnuo.framework.utils.MD5Utils;
import com.ibangnuo.framework.utils.PrimaryKeyUtil;
import com.ibangnuo.framework.web.BaseController;
import com.ibangnuo.manage.dao.UserMapper;
import com.ibangnuo.manage.dao.UserRepository;
import com.ibangnuo.manage.dao.UserRoleRepository;
import com.ibangnuo.manage.dao.employee.AgentRepository;
import com.ibangnuo.manage.dao.employee.CommercialRepository;
import com.ibangnuo.manage.dao.employee.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @Author: wwy
 * @Date: 2019-12-27 12:18
 */
@Service
public class UserService extends BaseController {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleRepository userRoleRepository;
    @Autowired
    private AgentRepository agentRepository;
    @Autowired
    private CommercialRepository commercialRepository;
    @Autowired
    private EmployeeRepository employeeRepository;

    /**
     * 根据用户名和密码查询
     *
     * @param username
     * @param password
     * @return
     */
    public User findUsernameAndPassword(String username, String password) {
        return userRepository.findByUsernameAndPassword(username, password);
    }

    /**
     * 根据用户查询
     *
     * @param username
     * @return
     */
    public User findByUsername(String username) {
        return userRepository.findByUsernameAndIsDelete(username, 1);
    }

    /**
     * 根据用户名，是否删除，状态
     * @param username
     * @return
     */
    public User findByUsernameNew(String username){
        return userRepository.findByUsernameAndIsDeleteAndStatus(username,1,1);
    }

    /**
     * 查询用户的权限和角色
     *
     * @param id
     * @return
     */
    public UserExt findByRoleAndPermission(String id) {
        return userMapper.findById(id);
    }

    /**
     * 根据用户名和type查询
     *
     * @param username
     * @return
     */
    public User findByUsernameAndType(String username) {
        HttpSession session = request.getSession();
        String type = String.valueOf(session.getAttribute("type"));
        User byUsernameAndType = userRepository.findByUsernameAndType(username, type);
        return byUsernameAndType;
    }

    /**
     * 根据用户名查找
     * @param username
     * @return
     */
    public ResponseResult findByNewUsername(String username){
        User user = userRepository.findByUsername(username);
        if(user == null){
            return new ResponseResult(CommonCode.SUCCESS);
        }
        return new ResponseResult(CommonCode.FAIL);
    }



}
